package com.neu.dao.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.neu.pojo.Room;
import com.neu.pojo.Worker;
import com.neu.service.impl.WorkerService;

public class RoomDao {
	//属性
	private File file = new File("room.json");//文件
	private ObjectMapper om = new ObjectMapper();//objmapper
	//单例
	private static RoomDao roomDao = new RoomDao();
	private RoomDao() {}
	public static RoomDao getInstance() {
		return roomDao;
	}

	public boolean add(Room room) {
		ArrayList<Room> rooms = new ArrayList<>();//房间容器
		
		try {
			rooms = this.om.readValue(file,new TypeReference<List<Room>>() {});//反序列化
			for(Room r:rooms) {//遍历
				if(r.getLocation().equals(room.getLocation())) {//房间已存在
					return false;//返回
				}
			}
			rooms.add(room);//添加
			om.writeValue(file, rooms);//序列化
			return true;//返回
		}catch(MismatchedInputException e) {//文件为空
			rooms.add(room);//加入
			try {
				om.writeValue(file, rooms);//序列化
			} catch (IOException e1) {//io异常
				e1.printStackTrace();
				return false;
			}
			return true;
		}catch (IOException e) {//io异常
			e.printStackTrace();
			return false;
		}
	}
	
	public ArrayList<Room> list(){
		ArrayList<Room> rooms = new ArrayList<>();//房间容器
		
		try {
			rooms = this.om.readValue(file,new TypeReference<List<Room>>() {});//反序列化
			return rooms;//返回
		}catch(MismatchedInputException e) {//文件为空
			return rooms;//返回
		}catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Room search(Room room) {
		ArrayList<Room> rooms = new ArrayList<>();//房间容器
		
		try {
			rooms = this.om.readValue(file,new TypeReference<List<Room>>() {});//反序列化
			for(Room r:rooms) {//遍历
				if(r.getLocation().equals(room.getLocation())) {//房间已找到
					return r;//返回
				}
			}
			return null;//返回
		}catch(MismatchedInputException e) {//文件为空
			return null;
		}catch (IOException e) {//io异常
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean delete(Room room) {
		ArrayList<Room> rooms = new ArrayList<>();//房间容器
		
		try {
			rooms = this.om.readValue(file,new TypeReference<List<Room>>() {});//反序列化
			for(int i=0;i<rooms.size();i++) {
				if(rooms.get(i).getLocation().equals(room.getLocation())&&rooms.get(i).getCurvalue() == 0) {
					rooms.remove(i);
					om.writeValue(file, rooms);//序列化
					return true;//返回
				}
			}
			return false;//返回
		}catch(MismatchedInputException e) {//文件为空
			return false;
		}catch (IOException e) {//io异常
			e.printStackTrace();
			return false;
		}		
	}
	
	public boolean livein(Room room) {
		ArrayList<Room> rooms = new ArrayList<>();//房间容器
		
		try {
			rooms = this.om.readValue(file,new TypeReference<List<Room>>() {});
			for(int i=0;i<rooms.size();i++) {
				if(rooms.get(i).getLocation().equals(room.getLocation())) {
					rooms.get(i).setCurvalue(rooms.get(i).getCurvalue()+1);
					break;
				}
			}
			om.writeValue(file, rooms);//序列化
			return true;
		} catch(MismatchedInputException e) {
			return false;
		}catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean liveout(Room room) {
		ArrayList<Room> rooms = new ArrayList<>();//房间容器
		
		try {
			rooms = this.om.readValue(file,new TypeReference<List<Room>>() {});
			for(int i=0;i<rooms.size();i++) {
				if(rooms.get(i).getLocation().equals(room.getLocation())) {
					rooms.get(i).setCurvalue(rooms.get(i).getCurvalue()-1);
					break;
				}
			}
			om.writeValue(file, rooms);//序列化
			return true;
		} catch(MismatchedInputException e) {
			return false;
		}catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
//	public static void main(String[] args) {
//		Room room = new Room("A201", "普通病房", 8, 0, WorkerService.getInstance().search(new Worker("2")));
//		System.out.println(RoomDao.getInstance().add(room));
//	}
}
